package com.iot.ht.controller.htrole.service;

import com.iot.core.data.BasePageDtQto;
import com.iot.core.data.BaseQto;
import com.iot.core.data.IdNameDto;
import com.iot.core.data.entity.HtRole;
import com.iot.core.data.entity.HtRoleMenu;
import com.iot.core.data.mapper.*;
import com.iot.core.data.service.ServiceImpl;
import com.iot.core.util.StreamUtil;
import com.iot.ht.controller.htpage.data.HtPageDto;
import com.iot.ht.controller.htrole.data.HtRoleDto;

import com.mybatisflex.core.paginate.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.iot.core.data.entity.table.HtDeptTableDef.HT_DEPT;
import static com.iot.core.data.entity.table.HtPageTableDef.HT_PAGE;
import static com.iot.core.data.entity.table.HtPlatTableDef.HT_PLAT;
import static com.iot.core.data.entity.table.HtRoleMenuTableDef.HT_ROLE_MENU;
import static com.iot.core.data.entity.table.HtRoleTableDef.HT_ROLE;
import static com.iot.core.data.entity.table.HtUserTableDef.HT_USER;

@Slf4j
@Service
public class HtRoleService extends ServiceImpl<HtRoleMapper, HtRole> implements IHtRoleService {
    @Autowired
    HtDbMapper htdbMapper;

    @Autowired
    HtRoleMenuMapper htRoleMenuMapper;
    @Autowired
    HtPlatMapper htPlatMapper;

    @Autowired
    HtDeptMapper htDeptMapper;

    @Override
    public Page<HtRoleDto> page(BasePageDtQto qm){
        var qw=getBaseMapper().getQueryWrapper();

        qw.select(HT_ROLE.ALL_COLUMNS)
                .select("b.Name as crtUname")
                .select("c.Name as crtDname")
                .select("d.Name as htplatName")
                .from(HT_ROLE)
                .leftJoin(HT_USER).as("b")
                .on(HT_ROLE.CRT_UID.eq(HT_USER.ID))
                .leftJoin(HT_DEPT).as("c")
                .on(HT_ROLE.CRT_DID.eq(HT_DEPT.ID))
                .leftJoin(HT_PLAT).as("d")
                .on(HT_ROLE.HTPLAT_ID.eq(HT_PLAT.ID))
        ;

        if(StringUtils.hasText(qm.getDtb() )){
            qw.and( HT_ROLE.CRT_DT.ge(qm.getDB()));
        }

        if(StringUtils.hasText(qm.getDte() )){
            qw.and( HT_ROLE.CRT_DT.lt(qm.getDE()));
        }

        if(StringUtils.hasText(qm.getKey() )){
            if(StringUtils.hasText(qm.getKey() )){
                qw.and( HT_ROLE.NAME.like(qm.getKey())

                );

            }
        }
        var sort=qm.getSort();
        qw.orderBy( sort.toString()  );


        var r =getBaseMapper().pageAs(  qm,qw , HtRoleDto.class);
        return r;
    }
    @Override
    public HtRoleDto detail(BaseQto qm){
        var m=getById(qm);
        var mm=new HtRoleDto();
        getBaseMapper().entityToDto(m,mm);
        mm.setHtplatName( htdbMapper.getName("htplat",m.getHtplatId() ) );
        mm.setCrtUname( htdbMapper.getName("htuser",m.getCrtUid() ) );
        mm.setCrtDname( htdbMapper.getName("htdept",m.getCrtDid() ) );

        var qwrolemenu=htRoleMenuMapper.getQueryWrapper();
        qwrolemenu.select(HT_ROLE_MENU.HTMENU_ID)
                        .from(HT_ROLE_MENU)
                                .where(HT_ROLE_MENU.HTROLE_ID.eq(m.getId()))
                ;

        var lmenuid=htRoleMenuMapper.selectListByQueryAs(qwrolemenu,Long.class);

        mm.getHtmenuIds().addAll(lmenuid);

        return mm;
    }


    @Override
    public HtRoleDto insertInit(){
        var mm=new HtRoleDto();

        var mplat=htPlatMapper.getById(1000L);
        mm.setHtplatId(mplat.getId());
        mm.setHtplatName(mplat.getName() );
        return mm;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(HtRoleDto mm){

        var m = new HtRole();
        getBaseMapper().dtoToEntity(mm,m);

        for(var mmid: mm.getHtmenuIds())
        {
            var madst=new HtRoleMenu();
            madst.setId(htdbMapper.nextLong() );
            madst.setHtroleId(mm.getId() );
            madst.setHtmenuId(mmid );
            htRoleMenuMapper.insert(madst );
        }
        return insert(m);

    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public   int update(HtRoleDto mm){
        var m=getById(mm.getId());

        getBaseMapper().dtoToEntity(mm,m);

        var qwrolemenu=htRoleMenuMapper.getQueryWrapper();
        qwrolemenu.and(HT_ROLE_MENU.HTROLE_ID.eq( mm.getId() ) );
        var lold=htRoleMenuMapper.selectListByQuery(qwrolemenu);
        for(var mmid: mm.getHtmenuIds())
        {
            var madst = StreamUtil.get(lold, a->a.getHtmenuId().equals(mmid));
            if(ObjectUtils.isEmpty(madst)){
                madst=new HtRoleMenu();
                madst.setId(htdbMapper.nextLong() );
                madst.setHtroleId(mm.getId() );
                madst.setHtmenuId(mmid );
                htRoleMenuMapper.insert(madst );
            }
            else{

            }
        }



        for(var madb : lold)
        {
            var masrc= StreamUtil.get(  mm.getHtmenuIds(), a->a.equals( madb.getHtmenuId()) );
            if(ObjectUtils.isEmpty(masrc)){
                htRoleMenuMapper.deleteById(madb,true);
            }
        }

        return  updateById(m);
    }




    public List<IdNameDto> listByDeptId(BaseQto mm){
        var mdept=getBaseMapper().getById(mm  );
        var qw=getBaseMapper().getQueryWrapper();
        qw.select("id","name")
                        .from(HT_ROLE)
                                .orderBy("id");
        var r=getBaseMapper().selectListByQueryAs(qw,IdNameDto.class);
        return r;
    }

}
